using System;
using System.Collections.Generic;
using UnityEngine;
using Yoozoo.Managers.ResourceManagerV2.Framework;
using Yoozoo.Managers.ResourceManagerV2.Runtime;

namespace WorldMapBase.Tile.Pool
{
    public class WorldPrefabPool : ObjectPool<Transform>
    {
        private ResLoader _resLoader;
        private int _initNum;
        private string _resPath;
        protected GameObject _originObject;

        protected Transform root;
        
        public WorldPrefabPool(int initNum,int capacity,string resPath) : 
            base(capacity,true,1,5,10)
        {
#if UNITY_EDITOR
            var strs = resPath.Split('/');
            _name = "沙盘静态层对象池: " + strs[strs.Length - 1];
#endif
            _initNum = initNum;
            _resPath = resPath;

            root = WorldPoolManager.TerrainRoot;
            if (resPath.Contains("tree"))
            {
                root = WorldPoolManager.TreeRoot;
            }

            if (initNum > capacity)
            {
                //UnityEngine.Debug.LogError("默认容量小于初始化数量: " + _resPath);
            }
        }

        public void PreLoad(Variable variable,string path)
        {
            GameObject asset =(UnityEngine.Object)variable.GetValue() as GameObject;
            _originObject = asset;
            WorldPerformance.UpdateSandBoxMat(_originObject, path,true);
            //UnityEngine.Object.Instantiate(asset,GlobalValue.INVISIBLE_POSITION,Quaternion.identity,WorldPoolManager.OriginRoot) as GameObject;
            base.OnInit(_initNum);
        }
        
        public void PreLoad(GameObject asset,string path)
        {
            _originObject = asset;
            WorldPerformance.UpdateSandBoxMat(_originObject, path,true);
            //UnityEngine.Object.Instantiate(asset,GlobalValue.INVISIBLE_POSITION,Quaternion.identity,WorldPoolManager.OriginRoot) as GameObject;
            base.OnInit(_initNum);
        }
        
        public void PreLoad(Action completeAction)
        {
            
            if (_resLoader == null)
            {
                _resLoader = ResLoader.Alloc();
            }
            _resLoader.Add2Load(_resPath, typeof(GameObject), ((success, name, asset) =>
            {
                GameObject tmp = asset as GameObject;
                _originObject = tmp;
                    //GameObject.Instantiate(tmp,GlobalValue.INVISIBLE_POSITION,Quaternion.identity,WorldPoolManager.OriginRoot);
                base.OnInit(_initNum);
                completeAction?.Invoke();
            }));
            _resLoader.Load();
        }
        
        protected override Transform ConstructItem()
        {
            if (_originObject == null)
            {
                Debug.LogEditorError("WorldPrefabPool：资源还未加载完成");
                return null;
            }

            Transform objTransform = 
                GameObject.Instantiate(_originObject, GlobalValue.INVISIBLE_POSITION, Quaternion.identity,root).transform;
            return objTransform;
        }

        protected override void BeforeRecover(Transform obj)
        {
            obj.position = GlobalValue.INVISIBLE_POSITION;
        }

        protected override void OnRemoveItem(Transform trans)
        {
            if (trans == null || trans.gameObject == null)
            {
                int a = 0;
            }
            GameObject.Destroy(trans.gameObject);
        }

        public override void Destroy()
        {
            base.Destroy();
            if (_resLoader != null)
            {
                _resLoader.Recycle2Cache();
                _resLoader = null;
                _originObject = null;
            }
         
            
        }
        
        
    }
}
